﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace jsonar
{
	public static class _Ext
	{
        #region GetChild
        public static Arr GetChildArr(this Obj obj, string name)
		{
			if (obj == null) return null;
			var child = obj.getChild(name);
			if (child is Arr) return child.getValue(Arr.NullINSTANCE);
			return null;
		}
		public static Obj GetChildObj(this Obj obj, string name)
		{
			if (obj == null) return null;
			var child = obj.getChild(name);
			if (child is Obj) return child.getValue(Obj.NullINSTANCE);
			return null;
		}
		public static Arr GetChildArr(this Arr arr, int index)
		{
			if (arr == null) return null;
			var child = arr.getChild(index);
			if (child is Arr) return child.getValue(Arr.NullINSTANCE);
			return null;
		}
		public static Obj GetChildObj(this Arr arr, int index)
		{
			if (arr == null) return null;
			var child = arr.getChild(index);
			if (child is Obj) return child.getValue(Obj.NullINSTANCE);
			return null;
		}

        public static Json GetChild(this Json json, string name)
        {
            if (json == null) return null;
            return json.getChild(name);
        }
        public static Json GetChild(this Json json, int index)
        {
            if (json == null) return null;
            return json.getChild(index);
        }
        #endregion

        #region GetValue
        public static Obj GetValue(this Json json, Obj dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static Arr GetValue(this Json json, Arr dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static int GetValue(this Json json, int dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static uint GetValue(this Json json, uint dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static long GetValue(this Json json, long dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static float GetValue(this Json json, float dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static double GetValue(this Json json, double dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static bool GetValue(this Json json, bool dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        public static string GetValue(this Json json, string dft)
        {
            if (json == null) return dft;
            return json.getValue(dft);
        }
        #endregion


        public static bool HasChild(this Json json)
        {
            if (json == null) return false;
            return json.hasChild();
        }
        public static string ToStr(this Json json)
        {
            if (json == null) return "null";
            return json.ToString();
        }
        public static string ToHtml(this Json json)
        {
            if (json == null) return "null";
            return json.toHtml();
        }
    }
}
